﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.WebControls;
using CreditAgricole.Framework;
using System.Web.UI.WebControls;

namespace CreditAgricole.PageCodeBehind
{
    public class Activities : ContextPage
    {
        #region Properties

        protected Panel pnlDisplayViews;
        protected DropDownChoiceField activityToDisplay;
        protected AddItemButton addItemTop;
        protected AddItemButton addItemBottom;
        private BaseViewsWebPart ViewsWebpart;
        
        #endregion

        protected void Page_Init(object sender, EventArgs e)
        {
            string activityType = MappingTool.CatchNull(activityToDisplay.ItemFieldValue);
            switch (activityType)
            {
                case Constants.ChangingListTitle :
                    {
                        addItemTop.ListType = Constants.ListType.ProductionDeployment;
                        addItemBottom.ListType = Constants.ListType.ProductionDeployment;
                        Guid webId = SPHelper.GetListWebId(Constants.ListType.ProductionDeployment);
                        if (webId != Guid.Empty)
                        {
                            ViewsWebpart = new MepViewsWebPart();
                            InitControls(webId, Constants.ChangingListTitle);
                        }
                        break;
                    }
                case Constants.KnowledgeApprovedListTitle :
                    {
                        addItemTop.ListType = Constants.ListType.KnowledgeUnapproved;
                        addItemBottom.ListType = Constants.ListType.KnowledgeUnapproved;
                        Guid webId = SPHelper.GetListWebId(Constants.ListType.KnowledgeApproved);
                        if (webId != Guid.Empty)
                        {
                            ViewsWebpart = new BKnowledgeViewsWebpart();
                            InitControls(webId, Constants.KnowledgeApprovedListTitle);
                        }
                        break;
                    }
                default:
                    {
                        addItemTop.ListType = Constants.ListType.KnowledgeUnapproved;
                        addItemBottom.ListType = Constants.ListType.KnowledgeUnapproved;
                        Guid webId = SPHelper.GetListWebId(Constants.ListType.KnowledgeApproved);
                        if (webId != Guid.Empty)
                        {
                            ViewsWebpart = new BKnowledgeViewsWebpart();
                            InitControls(webId, Constants.KnowledgeApprovedListTitle);
                        }
                        break;
                    }
            }
        }

        private void InitControls(Guid webId, string listTitle)
        {
            addItemTop.WebServerRelativeUrl = webRelativeUrl;
            addItemBottom.WebServerRelativeUrl = webRelativeUrl;
            addItemTop.ListTitle = listTitle;
            addItemBottom.ListTitle = listTitle;
            addItemTop.WebId = webId;
            addItemBottom.WebId = webId;
            ViewsWebpart.WebId = webId;
            ViewsWebpart.SiteId = SPContext.Current.Site.ID;
            pnlDisplayViews.Controls.Add(ViewsWebpart);
        }

        /// <summary>
        /// Page Load event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            SelectMenuItem(SPContext.Current.ListItem.Title);
            this.Form.Controls.Add(this.Page.LoadControl("~/_controltemplates/CreditAgricole.CustomControl/AddItem.ascx"));
            if (ViewsWebpart != null)
            {
                ViewsWebpart.ViewsMenu.SelectedMenu = Constants.ElementsMenu.MenuItem1;
                ViewsWebpart.ViewsMenu.SelectedCriteria = Constants.ElementsMenuCriteria.FirstCriteria;
                if (!string.IsNullOrEmpty(Request.QueryString["selected"]))
                {
                    int selectedMenuIndex = MappingTool.CatchIntNull(Request.QueryString["selected"]);
                    if (selectedMenuIndex > 0)
                    {
                        Constants.ElementsMenu selectedItem = (Constants.ElementsMenu)selectedMenuIndex;
                        ViewsWebpart.ViewsMenu.SelectedMenu = selectedItem;
                    }
                    if (!string.IsNullOrEmpty(Request.QueryString["criteria"]))
                    {
                        int selectedCritiriaIndex = MappingTool.CatchIntNull(Request.QueryString["criteria"]);
                        Constants.ElementsMenuCriteria selectedCriteria = (Constants.ElementsMenuCriteria)selectedCritiriaIndex;
                        if (selectedCritiriaIndex > 0)
                        {
                            ViewsWebpart.ViewsMenu.SelectedCriteria = selectedCriteria;
                        }
                    }
                }
            }
        }
    }
}
